package com.example.shopping1902;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping1902.fpage.FirstPageFragment;
import com.example.shopping1902.goods.GoodsFragment;
import com.example.shopping1902.me.LoginActivity;
import com.example.shopping1902.me.PersonCenterActivity;
import com.example.shopping1902.me.UserBean;
import com.example.shopping1902.utils.SaveUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    RadioGroup mainRg;
    List<Fragment> fList;
    FragmentManager fmanager;
    Button syBtn,typeBtn,meBtn,logBtn,regBtn,exitBtn;
    TextView logTv;//登录
    DrawerLayout drawerLayout;
    RadioButton syRb,typeRb,meRb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);//设置监听
        initFrag();
        ininView();
        //设置左侧抽屉代码
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (judgeLoginStatus()) {
            UserBean.DataBean userinfo = SaveUserUtils.getUserinfo();
            String nickname = userinfo.getNickname();
            logTv.setText(nickname+"用户,\n欢迎您!");
        }else {
            logTv.setText("未登录，请先登录！");
        }
    }

    //将左边抽屉里的控件初始化
    private void ininView() {
        syRb = findViewById(R.id.main_rb_fpage);
        typeRb = findViewById(R.id.main_rb_prod);
        meRb = findViewById(R.id.main_rb_me);

        syBtn =findViewById(R.id.main_left_btn_fp);
        meBtn =findViewById(R.id.main_left_btn_me);
        typeBtn =findViewById(R.id.main_left_btn_type);
        logBtn =findViewById(R.id.main_left_btn_login);
        regBtn =findViewById(R.id.main_left_btn_reg);
        exitBtn =findViewById(R.id.main_left_btn_exit);
        logTv =findViewById(R.id.main_left_tv1);
        drawerLayout = findViewById(R.id.main_drawer);
        syBtn.setOnClickListener(this);
        meBtn.setOnClickListener(this);
        typeBtn.setOnClickListener(this);
        logBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);
        exitBtn.setOnClickListener(this);

    }

    private void initFrag() {
        fList = new ArrayList<>();
        fList.add(new FirstPageFragment());
        fList.add(new GoodsFragment());
        fList.add(new MeFragment());

        fmanager = getSupportFragmentManager();
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0; i < fList.size(); i++) {
            ft.add(R.id.main_layout, fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();
    }
    //切换Fragment的方法
    private void changeFrag(int pos){
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0; i < fList.size(); i++) {
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.main_rb_fpage:
                changeFrag(0);
                break;
            case R.id.main_rb_prod:
                changeFrag(1);
                break;
            case R.id.main_rb_me:
                changeFrag(2);

                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_left_btn_fp:
                syRb.setChecked(true);
                break;
            case R.id.main_left_btn_type:
                typeRb.setChecked(true);
                break;
            case R.id.main_left_btn_me:
                meRb.setChecked(true);
                break;
            case R.id.main_left_btn_login://登录
                stareLoginPage(0);
                break;
            case R.id.main_left_btn_reg:
               stareLoginPage(1);
                break;
            case R.id.main_left_btn_exit:
                UserBean.DataBean bean = SaveUserUtils.getUserinfo();
                if (bean != null) {
                    SaveUserUtils.delUserInfo();
                    SaveUserUtils.deleteUserInfo(this, bean);
                    Toast.makeText(this, "注销成功！", Toast.LENGTH_SHORT).show();
                    logTv.setText("未登录，请先登录！");
                    MeFragment frag = (MeFragment)fList.get(2);
                    frag.onResumePage();
                } else {
                    Toast.makeText(this, "用户未登录！", Toast.LENGTH_SHORT).show();
                }
                break;
        }

        drawerLayout.closeDrawers();//关闭抽屉
    }
    //判断当前应用是否处于登录状态，如果处于登录状态就不跳转登录界面了，否则我们就跳转到登录界面
    public boolean judgeLoginStatus() {
        UserBean.DataBean bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean = SaveUserUtils.getUserToObj(this);
            if (bean == null) {
                return false;
            } else {
                return true;
            }
        }
        return true;
    }
    //跳转登录页面
    public void stareLoginPage(int flag){
        if (judgeLoginStatus()) {//已经登录
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息").setMessage("用户已登录，是否跳转到个人中心页面").setIcon(R.mipmap.icon);
            builder.setNegativeButton("取消",null);
            builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //跳转个人中心页面
                    Intent intent = new Intent(MainActivity.this, PersonCenterActivity.class);
                    startActivity(intent);
                }
            });


            builder.create().show();

        }else {//没有登录
            Intent it = new Intent(this, LoginActivity.class);
            it.putExtra("login",flag);
            startActivity(it);

        }
    }
    //页面当中相关碎片初始化的工作
}